package com.yinhai.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.androidquery.AQuery;
import com.yinhai.adapter.SettingListAdapter;
import com.yinhai.cfg.LoginConfig;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.model.ContactsUser;
import com.yinhai.service.ContactUserServiceImpl;

/**
 * SettingActivity 个人设置 lizhong
 * 
 * @author
 */
public class SettingActivity extends Activity {
	private SettingActivity thisRef;
	private AQuery aq;
	private SettingListAdapter listAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_setting);
		thisRef = this;
		aq = new AQuery(this);

		listAdapter = new SettingListAdapter(this);
		aq.id(R.id.listview).adapter(listAdapter).itemClicked(itemClick);
		LoadData();
	}

	private void LoadData() {
		ArrayList<List<String>> listdata = new ArrayList<List<String>>();

		List<String> list = new ArrayList<String>();
		listAdapter.clear();
		LoginConfig log = LoginConfigUtil.getConfig(thisRef);
		List<ContactsUser> ur = new ContactUserServiceImpl(thisRef)
				.findOnlineContactsUserByPhone(log.getUsername());
		for (int i = 0; i < ur.size(); i++) {
			list = new ArrayList<String>();
			list.add(ur.get(i).getName());
			list.add(ur.get(i).getGroupName());
			list.add(ur.get(i).getTelPhone());
			list.add(ur.get(i).getGroupId());
			listdata.add(list);
		}

		for (int i = 0; i < listdata.size(); i++) {
			listAdapter.addItem(listdata.get(i));
		}
		listAdapter.notifyDataSetChanged();
	}

	/**
	 * 列表项点击事件
	 * */
	OnItemClickListener itemClick = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int index,
				long arg3) {
			Intent it = new Intent(thisRef, SettingDetailActivity.class);
			List<String> list = listAdapter.getItem(index);
			it.putExtra("groupid", list.get(3));
			startActivity(it);

		}
	};

	protected void onResume() {
		super.onResume();
		LoadData();
	};

	public void doBack(View view) {
		onBackPressed();
	}

}
